let v = new Vue({
    el: '#app',
    data() {
        return {
            categoryArr:[],
            brandArr:[],
            dialogImageUrl: '',
            dialogVisible: false,
            product:{
                categoryId:"",
                brandId:"",
                code:"",
                title:"",
                shortTitle:"",
                content:"",
                url:"",
                tags:""
            },
        };
    },

    created:function(){

        //分类列表
        axios.get("/admin/category/list").then(function (response) {
            v.categoryArr = response.data.data;
        })

        //品牌列表
        axios.get("/admin/brand/list").then(function (response) {
            v.brandArr = response.data.data;
        })



        if (location.search.lastIndexOf('id')!=-1){
            let id  = location.search.split("=")[1];
        }
    },

    methods: {
        changeCategory(categoryId){
            v.categoryId = categoryId;
        },

        changeBrand(brandId){
            v.brandId = brandId;
        },

        handleRemove(file, fileList) {
            console.log(file, fileList);
            axios.get("/remove?name=" + file.response).then(function (response) {
                console.log("删除完成!");
                v.url="";
            })

        },
        handlePictureCardPreview(file) {
            this.dialogImageUrl = file.url;
            this.dialogVisible = true;
        },
        handleSuccess(response,file, fileList) {
            //response代表上传成功时服务器返回的图片路径
            v.url=response;
        },
        send(){
            //判断是否选择了图片
            if (v.url==""){
                v.$message.error("请选择上传的图片!");
                return;
            }


            v.product.url = v.url;//把图片路径添加到product对象中
            v.product.code = v.product.brandId + "" + v.product.categoryId;//组合code字段

            //发请求并把所有商品相关数据提交
            axios.post("/admin/add/product",v.product).then(function () {
                //发布完成后跳转回管理页面 传参3-1是为了让管理页面直接显示商品列表
                location.href="/admin/admin.html?index=3-1";
            })
        },
        goBack(){
            history.back();//返回上一页面
        },
        exceed(){
            v.$message.error("图片只能选择1个!");
        }
    }
})